package com.itcast.controller;

import com.itcast.bean.account.request.AccountRequestBean;
import com.itcast.pojo.Account;
import com.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@RequestMapping("/account")
public class AccountController extends BaseController {

    @Autowired
    AccountService accountService;

    @RequestMapping(value = "/findAll",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String findAll(HttpServletRequest request){
        String respBody=JSONArray.toJSONString(accountService.findAll());
        request.setAttribute(RESPONSEKEY,respBody);                              //此处每次需要手动将相应字符串设置到某个位置(此处为request中)，然后在拦截器里面统一打印出来，有什么办法可以优化，不用反复谢这个代码
        return respBody;
    }

    @RequestMapping(value = "/save",method = {RequestMethod.GET,RequestMethod.POST},consumes = "application/json")
    @ResponseBody
    public String save(HttpServletRequest request,@RequestBody AccountRequestBean accountRequestBean){
        String respBody=JSONArray.toJSONString(accountService.findAll());           //此处每次需要手动将相应字符串设置到某个位置(此处为request中)，然后在拦截器里面统一打印出来，有什么办法可以优化，不用反复谢这个代码
        request.setAttribute(RESPONSEKEY,respBody);
        return respBody;
    }

    @RequestMapping(value = "/test",method = {RequestMethod.GET,RequestMethod.POST},consumes = "multipart/form-data")
    @ResponseBody
    public String save(HttpServletRequest request,@RequestPart("files") MultipartFile[] files,@RequestPart("params") Account[] params){                                //表单内的key和参数列表一一对应，key的数目和名称固定。有没有办法让key的数目和名称不固定
        String respBody="upload success";
        request.setAttribute(RESPONSEKEY,respBody);
        System.out.println(params[0].getId());
        System.out.println(params[0].getName());
        System.out.println(params[0].getMoney());
        return respBody;
    }

    @RequestMapping(value = "/test1",method = {RequestMethod.GET,RequestMethod.POST},consumes = "multipart/form-data")
    @ResponseBody
    public String save1(HttpServletRequest request,@RequestPart("files") MultipartFile[] files,@RequestParam("params") String[] params){                                //表单内的key和参数列表一一对应，key的数目和名称固定。有没有办法让key的数目和名称不固定
        String respBody="upload success";
        request.setAttribute(RESPONSEKEY,respBody);
        System.out.println(params);
        return respBody;
    }

    @RequestMapping(value = "/test2",method = {RequestMethod.GET,RequestMethod.POST},consumes = "multipart/form-data")
    @ResponseBody
    public String save2(HttpServletRequest request,@RequestPart("files") MultipartFile[] files,@RequestParam("params") String params){                                //表单内的key和参数列表一一对应，key的数目和名称固定。有没有办法让key的数目和名称不固定
        String respBody="upload success";
        request.setAttribute(RESPONSEKEY,respBody);
        System.out.println(params);
        return respBody;
    }

    @RequestMapping(value = "/test3",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String test3(HttpServletRequest request){

        return "你好";
    }

    @RequestMapping(value = "/test4",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String test4(HttpServletRequest request){
        int a=1/0;

        return "你好";
    }

    static byte[]data=null;
    @RequestMapping(value = "/test5",method = {RequestMethod.GET,RequestMethod.POST})
    public void test5(HttpServletRequest request, HttpServletResponse response){


        //查出原始图片的数组，原始图片的链接

        try {
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(new File("d:/a.jpg")));
            data=new byte[bufferedInputStream.available()];
            int num=-1;
            while ((num=bufferedInputStream.read(data))!=-1){

            }

            response.setContentType("image/jpeg");
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(response.getOutputStream());
            bufferedOutputStream.write(data,0,data.length);
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}